#!/usr/bin/env python3

import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class WelcomePublisher(Node):
    
    def __init__(self):
        super().__init__('welcome_publisher')
        self.publisher_ = self.create_publisher(String, 'welcome_topic', 10)
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.get_logger().info('Publisher node has been started. Publishing message every 500ms')
    
    def timer_callback(self):
        msg = String()
        msg.data = 'Welcome to the ROS world'
        self.publisher_.publish(msg)
        self.get_logger().info(f'Publishing: "{msg.data}"')


def main(args=None):
    rclpy.init(args=args)
    
    welcome_publisher = WelcomePublisher()
    
    try:
        rclpy.spin(welcome_publisher)
    except KeyboardInterrupt:
        pass
    
    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    welcome_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()